# Guess, Andrew and Alexander Coppock. Does Counter-Attitudinal Information Cause Backlash? Results from Three Large Survey Experiments. British Journal of Political Science, forthcoming.

# Produces Table 4, Table 5, and Table 6 in the main text
# Produdes Tables 1, 2, 11, 12 in the Appendix

# Study 2 (Minimum Wage)

rm(list = ls())
library(tidyverse)
library(reshape2)
library(estimatr)
library(stargazer)
library(xtable)

load("GC_study_2.rdata")
source("GC_helpers.R")

# Table 4 -----------------------------------------------------------------

tab_1 <- 
  study_2  %>%
  group_by(condition_type) %>%
  summarise(n = format_num(n(), 0),
            positive_information = format_num(mean(positive_information), 0),
            negative_information = format_num(mean(negative_information), 0))

study_2_sub <- study_2 %>% select(condition_type, favor_T2_recode, amount_T2)

tab_2 <- 
  study_2_sub  %>%
  group_by(condition_type) %>%
  summarise_all(funs(mean, sd))%>%
  mutate_if(is_bare_numeric, format_num, digits = 2)

tab_3 <- 
  map(1:1000, ~study_2_sub %>% sample_n(size = nrow(study_2_sub), replace = TRUE)) %>%
  map(~ group_by(., condition_type) %>% summarise_all(funs(mean, sd))) %>%
  bind_rows(.id = "sim") %>%
  melt(id.vars = c("sim", "condition_type")) %>%
  dcast(condition_type ~ variable, fun.aggregate = sd) %>%
  mutate_if(is_bare_numeric, add_parens, digits = 2)


summary_df <- 
  tab_1 %>%
  left_join(tab_2) %>%
  bind_rows(tab_3) %>%
  mutate(condition_type = factor(condition_type, 
                                 labels=c("Placebo", 
                                          "Con Young / Con Old", "Pro Old / Con Old", 
                                          "Pro Old / Con Young", "Pro Young / Con Old",
                                          "Pro Young / Con Young", "Pro Young / Pro Old"))) %>%
  
  arrange(condition_type)

summary_df <- 
  within(summary_df,{
    condition_type[is.na(n)] <- NA
  })


summary_df <- summary_df[,c("condition_type", "n", "positive_information", "negative_information" ,
                            "favor_T2_recode_mean","favor_T2_recode_sd", 
                            "amount_T2_mean","amount_T2_sd")]

print.xtable(
  xtable(summary_df),
  hline.after = c(),
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE
)


# Table 5 -----------------------------------------------------------------

fit_1 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, initial_position == "con_raise"))
fit_2 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, initial_position == "con_raise"))

fit_3 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, initial_position == "inconsistent_raise"))
fit_4 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, initial_position == "inconsistent_raise"))

fit_5 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, initial_position == "pro_raise"))
fit_6 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age +  male + ideology + partyid + education + amount_T1,
            data = subset(study_2, initial_position == "pro_raise"))


stargazer(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6, 
          se=starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("amount|age|male|ideology|educ|race|party|favor"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Opponents","Among Moderates","Among Proponents"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Amount",
          covariate.labels=c("Pos. Info (0 to 1)", "Neg. Info (0 to 1)", "Condition: Placebo", "Constant"),
          model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Placebo condition is coded 0.",
                  "Covariates include T1 Amount, T1 Favor, age, gender, ideology, party ID, and education."),
          font.size="small",float=FALSE)

# Table 6 -----------------------------------------------------------------

fit_7 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, initial_position == "con_raise"))
fit_8 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, initial_position == "con_raise"))

fit_9 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, initial_position == "inconsistent_raise"))
fit_10 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
               age + male + ideology + partyid + education + amount_T1,
             data = subset(study_2, initial_position == "inconsistent_raise"))

fit_11 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
             data = subset(study_2, initial_position == "pro_raise"))
fit_12 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
               age +  male + ideology + partyid + education + amount_T1,
             data = subset(study_2, initial_position == "pro_raise"))

stargazer(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12, 
          se=starprep(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12),
          p=fix_ps(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("amount|age|male|ideology|educ|race|party|favor"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Opponents","Among Moderates","Among Proponents"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Favor",
          covariate.labels=c("Pos. Info (0 to 1)", "Neg. Info (0 to 1)", "Condition: Placebo", "Constant"),
          model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Placebo condition is coded 0.",
                  "Covariates include T1 Amount, T1 Favor, age, gender, ideology, party ID, and education."),
          font.size="small",float=FALSE)

# Appendix Tables: Table 1 (Attitude Extremity) --------------------------------------------------

study_2 <-
  within(study_2,{
    extr <-cut(amount_T1, breaks = quantile(amount_T1, probs = c(0, .25, .5, .75, 1)), labels = c(-2, -1, 1, 2))
    extr <- as.numeric(extr %in% c(-2, 2))
  })


fit_1 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, extr == 1))
fit_2 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, extr == 1))

fit_3 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, extr == 0))
fit_4 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, extr == 0))

stargazer(fit_1, fit_2, fit_3, fit_4, 
          se=starprep(fit_1, fit_2, fit_3, fit_4),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("amount|age|male|ideology|educ|race|party|favor"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Extreme Initial Position","Moderate Initial Position"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Amount",
          covariate.labels=c("Pos. Info (0 to 1)", "Neg. Info (0 to 1)", "Condition: Placebo", "Constant"),
          model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Placebo condition is coded 0.",
                  "Covariates include T1 Amount, T1 Favor, age, gender, ideology, party ID, and education."),
          font.size="small",float=FALSE)

# Appendix Tables: Table 2 (Attitude Extremity) --------------------------------------------------

fit_1 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, extr == 1))
fit_2 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, extr == 1))

fit_3 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, extr == 0))
fit_4 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + partyid + education + amount_T1,
            data = subset(study_2, extr == 0))

stargazer(fit_1, fit_2, fit_3, fit_4, 
          se=starprep(fit_1, fit_2, fit_3, fit_4),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("amount|age|male|ideology|educ|race|party|favor"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Extreme Initial Position","Moderate Initial Position"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Favor",
          covariate.labels=c("Pos. Info (0 to 1)", "Neg. Info (0 to 1)", "Condition: Placebo", "Constant"),
          model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Placebo condition is coded 0.",
                  "Covariates include T1 Amount, T1 Favor, age, gender, ideology, party ID, and education."),
          font.size="small",float=FALSE)

# Appendix Tables: Table 11 (Partisanship) ------------------------------------

fit_1 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data= subset(study_2, partyid=="democrat"))
fit_2 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + initial_position + education + amount_T1,
            data= subset(study_2, partyid=="democrat"))

fit_3 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data= subset(study_2, partyid=="independent"))
fit_4 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + initial_position + education + amount_T1,
            data= subset(study_2, partyid=="independent"))

fit_5 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data= subset(study_2, partyid=="republican"))
fit_6 <- lm(amount_T2 ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age +  male + ideology + initial_position + education + amount_T1,
            data= subset(study_2, partyid=="republican"))

stargazer(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6, 
          se=starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
          p=fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
          style="apsr", column.sep.width = "0pt",  digits=2,
          omit=c("amount|age|male|ideology|educ|race|initial_position|favor"), omit.labels=c("Covariates"),
          omit.stat=c("adj.rsq", "f", "ser"),
          column.labels =c("Among Democrats","Among Independents","Among Republicans"),
          column.separate = c(2,2,2),
          dep.var.labels="Dependent Variable: T2 Amount",
          covariate.labels=c("Pos. Info (0 to 1)", "Neg. Info (0 to 1)", "Condition: Placebo", "Constant"),
          model.numbers=FALSE,
          notes=c("Robust standard errors are in parentheses.",
                  "The information content of the Placebo condition is coded 0.",
                  "Covariates include T1 Amount, T1 Favor, age, gender, ideology, initial position, and education."),
          font.size="small",float=FALSE)

# Appendix Tables: Table 12 (Partisanship) ------------------------------------

fit_7 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, partyid == "democrat"))
fit_8 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
              age + male + ideology + initial_position + education + amount_T1,
            data = subset(study_2, partyid == "democrat"))

fit_9 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
            data = subset(study_2, partyid == "independent"))
fit_10 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
               age + male + ideology + initial_position + education + amount_T1,
             data = subset(study_2, partyid == "independent"))

fit_11 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo"), 
             data = subset(study_2, partyid == "republican"))
fit_12 <- lm(favor_T2_recode ~ positive_information + negative_information + (condition_type=="Placebo") + favor_T1_recode +
               age +  male + ideology + initial_position + education + amount_T1,
             data = subset(study_2, partyid == "republican"))


stargazer(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12, 
          se = starprep(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12), 
          p = fix_ps(fit_7, fit_8, fit_9, fit_10, fit_11, fit_12), 
          style = "apsr", column.sep.width = "0pt",  digits = 2, 
          omit=c("amount|age|male|ideology|educ|race|initial_position|favor"), omit.labels=c("Covariates"),
          omit.stat = c("adj.rsq", "f", "ser"), 
          column.labels = c("Among Democrats", "Among Independents", "Among Republicans"), 
          column.separate = c(2,2,2),
          dep.var.labels = "Dependent Variable: T2 Favor", 
          covariate.labels = c("Pos. Info (0 to 1)",
                               "Neg. Info (0 to 1)",
                               "Condition: Placebo",
                               "Constant"), 
          model.numbers = FALSE, 
          notes = c(
            "Robust standard errors are in parentheses.",
            "The information content of the Placebo condition is coded 0.",
            "Covariates include T1 Amount, T1 Favor, age, gender, ideology, initial position, and education."
          ), 
          font.size = "small", float = FALSE)

